package com.itheima.mvc.controller;

import com.itheima.mvc.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

//@RestController
@Controller
@RequestMapping("user")
@SessionAttributes(names = {"name"}) //只支持页面的形式 Model页面传参
public class UserController {

    @RequestMapping("findUser")
    @ResponseBody
    //将返回值 已json的数据返回，前提是 已经导入了json的包
    public User findUser(Long id){
        User user = new User();
        user.setName("zhangsan");
        user.setAge(15);
        user.setId(id);
        return user;
    }

    @RequestMapping("findUserList")
    @ResponseBody
    //将返回值 已json的数据返回，前提是 已经导入了json的包
    public List<User> findUserList(Long id){
        List<User> userList = new ArrayList<>();
        User user = new User();
        user.setName("zhangsan");
        user.setAge(15);
        user.setId(id);
        userList.add(user);
        return userList;
    }

    @RequestMapping("findUserList1")
    @ResponseBody
    //将返回值 已json的数据返回，前提是 已经导入了json的包
    public List<User> findUserList1(Long id,
                                    HttpServletRequest request,
                                    HttpServletResponse response,
                                    HttpSession session){
        System.out.println(request.getRequestURI());
        System.out.println(session);
        List<User> userList = new ArrayList<>();
        User user = new User();
        user.setName("zhangsan");
        user.setAge(15);
        user.setId(id);
        userList.add(user);
        return userList;
    }

    @RequestMapping("servlet")
    @ResponseBody
    //将返回值 已json的数据返回，前提是 已经导入了json的包
    public List<User> servlet(Long id,@RequestHeader("Accept-Encoding") String header,
                              @CookieValue("JSESSIONID") String jsessionId,
                              @SessionAttribute("name") String username){
        System.out.println("header="+header);
        System.out.println("cookie jessionId="+jsessionId);
        System.out.println("用户登录了，session username="+username);
        List<User> userList = new ArrayList<>();
        User user = new User();
        user.setName("zhangsan");
        user.setAge(15);
        user.setId(id);
        userList.add(user);
        return userList;
    }

//    @RequestMapping("login")
//    public String login(HttpSession httpSession){
//        httpSession.setAttribute("name","zhangsan");
//        return "success";
//    }

    @RequestMapping("login")
    public String login(Model model){
        model.addAttribute("name","lisi");
        return "page";
    }
}
